package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.dto.UserDto;
import com.sofwin.mapper.SysUserMapper;
import com.sofwin.pojo.SysUser;
import com.sofwin.pojo.SysUserExample;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private SysUserMapper mapper;

    /** 测试事务 */
    @Override
    public boolean saveUsers(List<SysUser> users) {
            int flag = 0;
            for (SysUser user : users) {
                flag +=mapper.insertSelective(user);
//                int i = 1/0;
            }

        return flag==users.size()?true:false;
    }

    /** 自定义查询 */
    @Override
    public SysUser selectUser(SysUser user) {
        SysUserExample ex = new SysUserExample();
        SysUserExample.Criteria criteria = ex.createCriteria();

        if (user!=null){
            if (user.getLoginAccount()!=null && !"".equals(user.getLoginAccount())){
                criteria.andLoginAccountEqualTo(user.getLoginAccount());
            }
            if (user.getLoginPwd()!=null &&!"".equals(user.getLoginPwd())){
                criteria.andLoginPwdEqualTo(user.getLoginPwd());
            }
        }
        List<SysUser> users = mapper.selectByExample(ex);
        if (users!=null && users.size()>0){
//            System.out.println(users.size());
//            System.out.println(users.get(0));
            return users.get(0);
        }
        return null;
    }

    /** 验证用户名和密码 */
    @Override
    public boolean checkLogin(SysUser user) {
        boolean stst=false;
        SysUser user1 = selectUser(user);
        if (user1!=null){
            stst=true;
        }
        return stst;
    }

    /**
     * 根据条件分页查询用户信息
     * @param user
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserDto> selectUserByPage(SysUser user, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<UserDto> users = mapper. selectUsers(user);
        return new PageInfo<UserDto>(users);
    }

    /**
    * @Author abiao
    * @Description 根据传过来的id，删除用户
    * @Date 2021/4/17 下午 4:38
    * @param ids id数组
    * @return boolean 是否删除成功
    */
    @Override
    public boolean deleteByIds(Integer[] ids) {
        int flag = 0;
        for (Integer id:ids){
            flag+=mapper.deleteByPrimaryKey(id);
        }
        return flag==ids.length?true:false;
    }

    /**
    * @Author abiao
    * @Description 新增|更新用户星系
    * @Date 2021/4/18 下午 6:04
    * @param user 目标用户
    * @return boolean 是否删除成功
    */
    @Override
    public boolean saveUser(SysUser user) {
        // put? null
        if (user.getId()!=null){
            return mapper.updateByPrimaryKeySelective(user)==1?true:false;
        }
        return mapper.insertSelective(user)==1?true:false;
    }

    /**
     * @Author abiao
     * @Description 通过id查询用户，用户编辑获取id
     * @Date 2021/4/17 下午 5:28
     * @param id 用户id
     * @return SysUser id对应用户的pojo
     */
    @Override
    public SysUser selectUserById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    /**
     * shiro认证，查看用户名是否存在
     * @param username
     * @return
     */
    @Override
    public boolean selectUserByName(String username) {
        SysUserExample ex = new SysUserExample();
        SysUserExample.Criteria criteria = ex.createCriteria();
        criteria.andLoginAccountEqualTo(username);
        List<SysUser> users = mapper.selectByExample(ex);
        if (users!=null && users.size()==1){
            return true;
        }
        return false;
    }

    /**
     * 根据角色Id查询角色拥有的细密度权限
     * @param roleId
     * @return
     */
    @Override
    public List<String> selectUserPermissionByRoleId(Integer roleId) {
        return mapper.selectRolePermission(roleId);
    }


}
